<template>
 <el-config-provider :locale="locale">
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>患者信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card >
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getPatientList" >
            <template #append>
              <el-button  :icon="Search" @click="getPatientList"/>
            </template>
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>
      
      <!-- 用户列表区域 -->
      <el-table :data="patientList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="PatientName" label="姓名"  />
        <el-table-column prop="PatientSex" label="性别"  />
        <el-table-column prop="PatientAge" label="年龄"  />
        <el-table-column prop="PatientTelNum" label="电话" />
        <el-table-column prop="PatientAddr" label="住址"  />
        <el-table-column prop="NickName" label="昵称" />
        <el-table-column  label="操作" >
          <!-- scope是一个插槽 -->
          <template #default="scope">  
            <!-- 编辑按钮 -->
            <el-button size="small" @click="showEditDialog( scope.row.id)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button size="small" type="danger" @click="deleteById( scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
     
      <!-- 分页区域 -->
      <el-pagination  v-model:currentPage="queryInfo.pagenum" v-model:page-size="queryInfo.pagesize"
      :page-sizes="[2, 3, 5, 10]" :small="small" :disabled="disabled" :background="background"
      layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange"  @current-change="handleCurrentChange" />
      
    </el-card>
    
    <!-- 添加用户对话框 -->
    <el-dialog v-model="addDialogVisible" title="添加患者" width="50%" @close="addDialogClosed">
      <!-- 表单内容区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="用户名" prop="PatientName">
          <el-input v-model="addForm.PatientName" />
        </el-form-item>
        <el-form-item label="性别" prop="PatientSex">
          <el-select  v-model="addForm.PatientSex" clearable placeholder="请选择性别" style="width:100%">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="PatientAge">
          <el-input v-model="addForm.PatientAge"/>
        </el-form-item>
        <el-form-item label="电话" prop="PatientTelNum">
          <el-input v-model="addForm.PatientTelNum" />
        </el-form-item>
        <el-form-item label="住址" prop="PatientAddr">
          <el-input v-model="addForm.PatientAddr"/>
        </el-form-item>
        <el-form-item label="昵称" prop="NickName">
          <el-input v-model="addForm.NickName" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addPatient">确定</el-button>
        </span>
      </template>
    </el-dialog>
    
    <!-- 修改用户对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑用户" width="50%" @close="editDialogClosed">
      <!-- 表格内容主体区域 -->
      <el-form ref="editFormRef" :model="editForm" :rules="editFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="用户名" >
          <el-input v-model="editForm.PatientName"  disabled/>
        </el-form-item>
        <el-form-item label="性别" >
          <el-input v-model="editForm.PatientSex"/>
        </el-form-item>
        <el-form-item label="年龄" >
          <el-input v-model="editForm.PatientAge"/>
        </el-form-item>
        <el-form-item label="电话" prop="PatientTelNum">
          <el-input v-model="editForm.PatientTelNum" />
        </el-form-item>
        <el-form-item label="住址" prop="PatientAddr">
          <el-input v-model="editForm.PatientAddr"/>
        </el-form-item>
        <el-form-item label="昵称" prop="NickName">
          <el-input v-model="editForm.NickName" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editPatientInfo">确定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
  </el-config-provider>
</template>


<script >
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { ArrowRight } from '@element-plus/icons-vue'
import { Search } from '@element-plus/icons-vue'
import qs from 'qs'
import {  ElMessageBox } from 'element-plus'
export default {
  components:{
    ArrowRight,
    ElConfigProvider,
    ElMessageBox

  },
  setup(){
    return{
      Search,
      locale: zhCn
    }
  },

  data(){
    //验证手机号的规则
    var checkTelNum=(rule,value,cb)=>{
      //验证手机号的正则表达式
      const regTelNum=/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if(regTelNum.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return{
      //获取用户列表的参数对象
      queryInfo:{
        query:'',
        //当前页数
        pagenum:1,
        //当前每页显示多少调数据
        pagesize:2,
        
      },
      patientList:[],
      total:0,
      //控制添加用户对话框的显示与隐藏
      addDialogVisible:false,
      //添加用户的表单数据
      addForm:{
        PatientName:'',
        PatientSex:'',
        PatientAge:'',
        PatientTelNum:'',
        PatientAddr:'',
        NickName:'',

      },
      //添加表单的验证规则对象
      addFormRules:{
        PatientName:[{
          required:true,message:'请输入名字',trigger:'blur'
        },{ min:2,max:5,message:'名字在2~5个字符之间' ,trigger:'blur'}],
        PatientSex:[{
          required:true,message:'请选择性别',trigger:'blur'
        }],
        PatientAge:[{
          required:true,message:'请输入年龄',trigger:'blur'
        }],
        PatientTelNum:[{
          required:true,message:'请输入电话',trigger:'blur'
        },{validator:checkTelNum,trigger:'blur'}],
        PatientAddr:[{
          required:true,message:'请输入住址',trigger:'blur'
        }],
        NickName:[{
          required:true,message:'请输入昵称',trigger:'blur'
        },{ min:1,max:5,message:'昵称在1~5个字符之间' ,trigger:'blur'}],
      },
      //控制编辑用户对话框的显示与隐藏
      editDialogVisible:false,
      //查询到用户的信息对象
      editForm:{},
      //修改表单的验证规则对象
      editFormRules:{
        PatientTelNum:[{
          required:true,message:'请输入电话',trigger:'blur'
        },{validator:checkTelNum,trigger:'blur'}],
        PatientAddr:[{
          required:true,message:'请输入住址',trigger:'blur'
        }],
        NickName:[{
          required:true,message:'请输入昵称',trigger:'blur'
        },{ min:1,max:5,message:'昵称在1~5个字符之间' ,trigger:'blur'}],
      
      },
    }
  },

  created(){
    this.getPatientList()
  },

  methods:{
    async getPatientList(){
      const {data:res} =await this.$http.get('/my/patientmessage/getpatient',{ params:this.queryInfo})
      if(res.meta.status!==200) {
        return this.$message.err('获取患者信息列表失败')
      }
      this.patientList=res.data.patient
      this.total=res.data.total
      //console.log(res)
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize){
      //console.log(newSize)
      this.queryInfo.pagesize=newSize
      this.getPatientList()

    },
    //监听页码值改变的事件
    handleCurrentChange(newPage){
      console.log(newPage)
      this.queryInfo.pagenum=newPage
      this.getPatientList()

    },
    //监听对话框的关闭事件resetFields()
    addDialogClosed(){
      this.$refs.addFormRef.resetFields()
    },
    //添加新用户
    addPatient(){
      this.$refs.addFormRef.validate(async valid=>{
        if(!valid)  return 
        //可以发出添加用户的网络请求
        const {data:res} =await this.$http.post('/my/patientmessage/addpatient',qs.stringify(this.addForm))
        //console.log(res)
        if(res.meta.status!==201){
          this.$message.Error('添加用户失败')
        }
        this.$message.success('添加用户成功')
        //隐藏用户对话框
        this.addDialogVisible=false
        //重新获取用户列表数据
        this.getPatientList()

      })
    },
    //展示编辑的对话框
    async showEditDialog(id){
      //console.log(id)
      const {data:res} =await this.$http.get('/my/patientmessage/getpatientbyid/'+id)
      console.log(res)
      if(res.meta.status!==200) { 
        return this.$message.error('查询患者信息失败')
      }
      this.editForm=res.data
      this.editDialogVisible=true
      
    },

    //监听修改用户对话框的关闭事件
    editDialogClosed(){
      this.$refs.editFormRef.resetFields()
    },
    //修改用户信息并提交
    editPatientInfo(){ 
      this.$refs.editFormRef.validate( async valid=>{
        if(!valid) return 
        //发起修改用户信息的数据请求
        //this.editForm=JSON.parse(JSON.stringify(this.editForm))
        var data = qs.stringify(this.editForm)
        const {data:res} = await this.$http.post('/my/patientmessage/updatepatient/'+ this.editForm.id, data,
        {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
        //判断请求是否成功
        if (res.meta.status !== 200) {
          return this.$message.error('更新患者信息失败！')
        }
        //关闭对话框
        this.editDialogVisible=false
        //刷新数据列表
        this.getPatientList()
        //提示修改成功
        this.$message.success('更新患者信息成功！')
  
      })
    },

    //根据id删除用户信息
    async deleteById(id){
      //console.log(id)
      //弹框提示是否删除数据
      const confirmResult=await ElMessageBox.confirm('此操作将永久删除该用户，是否继续?','提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(err=>err)
      //如果用户确认删除，返回字符串confirm
      //console.log(confirmResult)
      //如果用户取消了删除，返回值为字符串cancel
      if(confirmResult!=='confirm'){
      return  this.$message.info('已取消删除')
      }
      const {data:res} =await this.$http.get('/my/patientmessage/deletepatient/'+id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除信息失败！')
      }
      this.$message.success('删除信息成功！')
    }
   
    // search() {
    //   this.queryInfo.pagenum=1
    //   this.pageshow = false
    //   this.getPatientList()
    //   this.$nextTick(() => {
    //     this.pageshow = true
    //   })
    // }


  
  },

}
</script>

<style lang="less" scoped>

</style>